package com.chinasi.web.action.common.tag;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.map.ser.StdSerializerProvider;
import org.codehaus.jackson.map.ser.std.NullSerializer;

import com.caland.framework.core.utils.DateUtils;
import com.caland.framework.core.utils.SpringContextHolder;
import com.chinasi.model.ConsultBasic;
import com.chinasi.model.DictData;
import com.chinasi.model.ResTotal;
import com.chinasi.service.ResTotalService;
import com.chinasi.web.action.common.DictCache;
import com.chinasi.web.action.common.SystemConstants;
import com.chinasi.web.action.common.SystemConstants.BidType;

public class MyFunction {

	/** 招投标类型名称转换 */
	public static String getBidTypeName(String value){
		return BidType.getBidType(Integer.parseInt(value)).getName();
	}
	
	/** 显示指定长度文本，超过显示长度加省略号显示 */
	public static String shortText(String text, java.lang.Integer showCharCount){
		if(text.length() <= showCharCount){
			return text;
		}else{
			return text.substring(0, showCharCount) + "......";
		}
	}
	
	/** 将java对象序列化成json格式，方便在浏览器中传递 */
	public static String toJson(Object obj){
		
		String jsonString = "";
		try {
			StdSerializerProvider ssp = new StdSerializerProvider();
			ssp.setNullValueSerializer(NullSerializer.instance);
			ObjectMapper objectMapper = new ObjectMapper(null, ssp, null);
			/**
			 * 设置日期序列化及反序列化格式
			 */
			objectMapper.setDateFormat(new SimpleDateFormat(DateUtils.DATE_HH_MM_SS));
			
			objectMapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
			
			objectMapper.setSerializationInclusion(Inclusion.NON_NULL);
			
			jsonString = objectMapper.writeValueAsString(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonString;
		
	}
	
	/** 获取字典配置数据 */
	public static List getDictData(String code){
		return DictCache.getByCode(code, DictData.class);
	}
	
	/** 翻译dictData code */
	public static String getInfoTypeName(String dataCode){
		List<DictData> list = DictCache.getByCode(SystemConstants.DictCode.infoType.toString(), DictData.class);
		if(list != null){
			for(Iterator<DictData> it = list.iterator(); it.hasNext();){
				DictData dictData = it.next();
				if(dictData.getDictdataCode().equals(dataCode)){
					return dictData.getDictdataText();
				}
			}
		}
		return "";
	}
	
	/** 翻译dictData code */
	public static String getInfoTagName(String dataCode){
		List<DictData> list = DictCache.getByCode(SystemConstants.DictCode.infoTag.toString(), DictData.class);
		if(list != null){
			for(Iterator<DictData> it = list.iterator(); it.hasNext();){
				DictData dictData = it.next();
				if(dictData.getDictdataCode().equals(dataCode)){
					return dictData.getDictdataText();
				}
			}
		}
		return "";
	}
	
	/** 翻译dictData code */
	public static String getProjTypeName(String dataCode){
		List<DictData> list = DictCache.getByCode(SystemConstants.DictCode.projTyte.toString(), DictData.class);
		if(list != null){
			for(Iterator<DictData> it = list.iterator(); it.hasNext();){
				DictData dictData = it.next();
				if(dictData.getDictdataCode().equals(dataCode)){
					return dictData.getDictdataText();
				}
			}
		}
		return "";
	}
	
	public static String[] getResTotal(String id){
		ResTotalService resTotalService = SpringContextHolder.getBean(ResTotalService.class);
		List<ResTotal> list = resTotalService.findByOrgId(id);
		return new String[]{"100","20","10","5","1"};
	}
	
	public static void main(String[] args) {
		System.out.println(getBidTypeName("1"));
		
		System.out.println(toJson(new ConsultBasic()));
		
		Map<String,String> params = new HashMap<String, String>();
		params.put("id", "1");
		System.out.println(toJson(params));
		
	}
}
